import 'package:dynamic_stock/const/const.dart';
import 'package:dynamic_stock/model/car_list_mode.dart';
import 'package:dynamic_stock/model/login_model.dart';
import 'package:dynamic_stock/network/network.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CarinfoPage extends StatefulWidget {

  @override
  _CarinfoPageState createState() => _CarinfoPageState();
}

class _CarinfoPageState extends State<CarinfoPage> with AutomaticKeepAliveClientMixin {

  RefreshController _refreshController;
  List<Car> list = [];
  int pageIndex = 1;
  int pageSize = 10;

  @override
  void initState() {
    super.initState();
    _refreshController = RefreshController(initialRefresh: true);
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }
 
  _loadData() async {
    pageIndex = 1;
    UserData userData = await UserData.instance();
    NetworkTool().request_car_list(
      {"platComId":userData.platComId,"pageIndex":pageIndex, "pageSize":pageSize}
    ).then((res){
      if(res.errcode == 0 && res.data.list != null) {
        setState(() {
          list = res.data.list;
        });
      } else {
        print(res.message);
      }
    }).catchError((e){

    }).whenComplete((){
      if(_refreshController.isLoading) _refreshController.loadComplete();
      if(_refreshController.isRefresh) _refreshController.refreshCompleted(resetFooterState: true);
    });
  }

  _onLoadindMore() async{
    UserData user = await UserData.instance();
    pageIndex += 1;
    NetworkTool().request_car_list(
      {"platComId":user.platComId,"pageIndex":pageIndex, "pageSize":pageSize}
    ).then((res){
      if(res.errcode == 0) {
        setState(() {
          list.addAll(res.data.list);
        });
        if(_refreshController.isLoading) _refreshController.loadComplete();
        if(res.data.list.length < pageSize) _refreshController.loadNoData();
      } else {
        print(res.message);
      }
    }).catchError((e){
      if(_refreshController.isLoading) _refreshController.loadComplete();
    }).whenComplete((){
      if(_refreshController.isRefresh) _refreshController.refreshCompleted();
    });
  }

  final cardetailStyle = TextStyle(
    color: Color(0xff5F5D70),
    fontSize: 13,
  );

  Widget _createItem(Car car){
    return Container(
      color: AppMainColor.normalBackColor,
      child: Container(
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(25.0, 27, 25, 0),
              child: Row(
                children: <Widget>[
                  Image.asset('assets/car_icon.png'),
                  SizedBox(width: 13.5,),
                  Text(car.vehicleBrand, 
                  style: TextStyle(color: Color(0xff35343D), fontSize: 17, fontWeight: FontWeight.w500))
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(60.5, 5, 0, 0),
              child: Row(
                children: <Widget>[
                  Text(car.vehNo, style: cardetailStyle,),
                  SizedBox(width: 13.5,),
                  Container(width: 0.5, height: 11.5,color: Color(0xff5F5D70),),
                  SizedBox(width: 13.5,),
                  Text(car.userName ?? "--", style: cardetailStyle,),
                  SizedBox(width: 13.5,),
                  Text(car.stewardMobilePhone, style: cardetailStyle,),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(60.5, 20, 29, 26.5),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Text(car.stewardUserName,style: TextStyle(color: Color(0xff5F5D70),fontSize: 15, fontWeight: FontWeight.w500),),
                      SizedBox(height: 5,),
                      Text("车管家",style: TextStyle(color: Color(0xffB2B0C0),fontSize: 12)),
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      Text(car.finishOrderCount.toString(),style: TextStyle(color: Color(0xff00CA9D),fontSize: 15, fontWeight: FontWeight.w500),),
                      SizedBox(height: 5,),
                      Text("成绩单",style: TextStyle(color: Color(0xffB2B0C0),fontSize: 12)),
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Text(car.priceUnit == null ? "--" : car.priceUnit.toString(),style: TextStyle(color: Color(0xffF5A623),fontSize: 15),),
                          SizedBox(width: 3,),
                          Text("元/天",style: TextStyle(color: Color(0xff5F5D70),fontSize: 10),),
                        ],
                      ),
                      SizedBox(height: 5,),
                      Text("平日价",style: TextStyle(color: Color(0xffB2B0C0),fontSize: 12)),
                    ],
                  ),
                ],
              ),
            ),
            Container(color: AppMainColor.normalBackColor,height: 10,)
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("车辆信息", style: AppTextStyle.tabbarTitleStyle),
        elevation: 0.5,
      ),
      body: Container(
        color: AppMainColor.normalBackColor,
        child: SmartRefresher(
          enablePullDown: true,
          enablePullUp: true,
          header: WaterDropHeader(),
          footer: CustomFooter(
          builder: (BuildContext context,LoadStatus mode){
              Widget body ;
              if(mode==LoadStatus.idle){
                body =  Text("上拉加载更多");
              }
              else if(mode==LoadStatus.loading){
                body =  CupertinoActivityIndicator();
              }
              else if(mode == LoadStatus.failed){
                body = Text("加载失败请重试!");
              }
              else if(mode == LoadStatus.canLoading){
                  body = Text("松开加载更多");
              }
              else{
                body = Text("已经到底了");
              }
              return Container(
                height: 55.0,
                child: Center(child:body),
              );
            },
          ),
          controller: _refreshController,
          onRefresh: _loadData,
          onLoading: _onLoadindMore,
          child: ListView.builder(
            itemBuilder: (context, index){
              return _createItem(list[index]);
            },
            itemCount: list.length,
          ),
        ),
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}